package com.study.d016event;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/** 键盘事件
*@description
*@author xtayhame
*date 2020年3月7日
*version
*since v1.0
*/
public class KeyEventMain extends Application{

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox = new HBox(20);
		Button b1 = new Button("button1");
		Button b2 = new Button("Button02");
		TextField tf  =new TextField();
		TextField tf2  =new TextField();
		Rectangle rg = new Rectangle(100, 100);
		rg.setStyle("-fx-background-color:#ffff55");

		hBox.getChildren().addAll(b1,b2,tf,rg,tf2);

		anchorPane.getChildren().add(hBox);
		stage.setScene(new Scene(anchorPane, 800, 500));
		stage.show();

		//b1按钮的键盘事件，前提是按钮得到焦点,且为英文输入法
		b1.setOnKeyPressed(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				//getCharacter()方法只针对输入控件有效，比如TextField
				System.out.println("11==>"+event.getCharacter());
				//如果是中文输入法，则getCode()得到的是Undefined
				System.out.println("当前按键："+event.getCode().getName());
			}
		});

		//鼠标释放事件
		b1.setOnKeyReleased(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				//getCharacter()方法只针对输入控件有效，比如TextField
				System.out.println("22==>"+event.getCharacter());
				System.out.println("释放按钮："+event.getCode().getName());
			}
		});


		//文字输入键按下松开后响应
		//只会响应文字输入，不会响应Ctrl ,Shift键等
		b1.setOnKeyTyped(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				//此时可以使用getCharacter()获取
				System.out.println("getCharacter==>"+event.getCharacter());
			}
		});


		//当输入框获取焦点后，获取键盘输入（注意:输入中文无效）
		tf.setOnKeyTyped(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				System.out.println("输入的文本是:" + event.getCharacter());
			}
		});


		//矩形框添加键盘事件 会发现矩形框无法获取焦点
		rg.setOnKeyPressed(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				System.out.println("Rectangle: getCode="+event.getCode().getName());
			}
		});

		rg.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				System.out.println("设置焦点");
				rg.requestFocus();
			}
		});



		System.out.println("***************************完整例子********************************************");
		tf2.setOnKeyPressed(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				System.out.println("setOnKeyPressed:");
				System.out.println("	getCharacter()="+event.getCharacter());
				System.out.println("	getText()="+event.getText());
				System.out.println("	getCode()="+event.getCode().getName());
				System.out.println("	getSource()="+event.getSource());
				System.out.println("	getTarget()="+event.getTarget());
				System.out.println("	getEventType()="+event.getEventType());
			}
		});

		tf2.setOnKeyTyped(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				System.out.println("setOnKeyTyped:");
				System.out.println("	getCharacter()="+event.getCharacter());
				System.out.println("	getText()="+event.getText());
				System.out.println("	getCode()="+event.getCode().getName());
				System.out.println("	getSource()="+event.getSource());
				System.out.println("	getTarget()="+event.getTarget());
				System.out.println("	getEventType()="+event.getEventType());
			}
		});


		tf2.setOnKeyReleased(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				System.out.println("setOnKeyReleased:");
				System.out.println("	getCharacter()="+event.getCharacter());
				System.out.println("	getText()="+event.getText());
				System.out.println("	getCode()="+event.getCode().getName());
				System.out.println("	getSource()="+event.getSource());
				System.out.println("	getTarget()="+event.getTarget());
				System.out.println("	getEventType()="+event.getEventType());
			}
		});





	}

}


/*
 *
 *
 *
 *
 *
 *
 * */
